<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动ID" prop="activityId">
        <el-input
          v-model="queryParams.activityId"
          placeholder="请输入活动ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小程序用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入小程序用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件号码" prop="documentNumber">
        <el-input
          v-model="queryParams.documentNumber"
          placeholder="请输入证件号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="国家/地区" prop="country">
        <el-input
          v-model="queryParams.country"
          placeholder="请输入国家/地区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-input
          v-model="queryParams.gender"
          placeholder="请输入性别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出生年月" prop="birthDate">
        <el-date-picker clearable
          v-model="queryParams.birthDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出生年月">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="紧急联系人姓名" prop="emergencyContact">
        <el-input
          v-model="queryParams.emergencyContact"
          placeholder="请输入紧急联系人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="紧急联系人电话" prop="emergencyPhone">
        <el-input
          v-model="queryParams.emergencyPhone"
          placeholder="请输入紧急联系人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="与紧急联系人关系" prop="contactRelation">
        <el-input
          v-model="queryParams.contactRelation"
          placeholder="请输入与紧急联系人关系"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服装尺码" prop="clothingSize">
        <el-input
          v-model="queryParams.clothingSize"
          placeholder="请输入服装尺码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入详细地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户签名" prop="signature">
        <el-input
          v-model="queryParams.signature"
          placeholder="请输入用户签名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名费用,单位分" prop="registrationFee">
        <el-input
          v-model="queryParams.registrationFee"
          placeholder="请输入报名费用,单位分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ss:participants:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ss:participants:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ss:participants:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ss:participants:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="participantsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="参赛者报名主键ID" align="center" prop="ssParticipantsId" />
      <el-table-column label="活动ID" align="center" prop="activityId" />
      <el-table-column label="小程序用户ID" align="center" prop="userId" />
      <el-table-column label="证件类型，默认为居民身份证" align="center" prop="documentType" />
      <el-table-column label="证件号码" align="center" prop="documentNumber" />
      <el-table-column label="国家/地区" align="center" prop="country" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="出生年月" align="center" prop="birthDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="紧急联系人姓名" align="center" prop="emergencyContact" />
      <el-table-column label="紧急联系人电话" align="center" prop="emergencyPhone" />
      <el-table-column label="与紧急联系人关系" align="center" prop="contactRelation" />
      <el-table-column label="血型" align="center" prop="bloodType" />
      <el-table-column label="服装尺码" align="center" prop="clothingSize" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="用户签名" align="center" prop="signature" />
      <el-table-column label="报名费用,单位分" align="center" prop="registrationFee" />
      <el-table-column label="状态: 1(待审核)/2(已通过)/3(已付款)/4(已抽选)/5(已通知)/6(排队中)/7(已取消)/8(审核不通过)" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ss:participants:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ss:participants:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参赛者报名对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动ID" prop="activityId">
          <el-input v-model="form.activityId" placeholder="请输入活动ID" />
        </el-form-item>
        <el-form-item label="小程序用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入小程序用户ID" />
        </el-form-item>
        <el-form-item label="证件号码" prop="documentNumber">
          <el-input v-model="form.documentNumber" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="国家/地区" prop="country">
          <el-input v-model="form.country" placeholder="请输入国家/地区" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="出生年月" prop="birthDate">
          <el-date-picker clearable
            v-model="form.birthDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生年月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="紧急联系人姓名" prop="emergencyContact">
          <el-input v-model="form.emergencyContact" placeholder="请输入紧急联系人姓名" />
        </el-form-item>
        <el-form-item label="紧急联系人电话" prop="emergencyPhone">
          <el-input v-model="form.emergencyPhone" placeholder="请输入紧急联系人电话" />
        </el-form-item>
        <el-form-item label="与紧急联系人关系" prop="contactRelation">
          <el-input v-model="form.contactRelation" placeholder="请输入与紧急联系人关系" />
        </el-form-item>
        <el-form-item label="服装尺码" prop="clothingSize">
          <el-input v-model="form.clothingSize" placeholder="请输入服装尺码" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="用户签名" prop="signature">
          <el-input v-model="form.signature" placeholder="请输入用户签名" />
        </el-form-item>
        <el-form-item label="报名费用,单位分" prop="registrationFee">
          <el-input v-model="form.registrationFee" placeholder="请输入报名费用,单位分" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listParticipants, getParticipants, delParticipants, addParticipants, updateParticipants } from "@/api/ss/participants";

export default {
  name: "Participants",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参赛者报名表格数据
      participantsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityId: null,
        userId: null,
        documentType: null,
        documentNumber: null,
        country: null,
        name: null,
        gender: null,
        birthDate: null,
        phone: null,
        emergencyContact: null,
        emergencyPhone: null,
        contactRelation: null,
        bloodType: null,
        clothingSize: null,
        address: null,
        signature: null,
        registrationFee: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        activityId: [
          { required: true, message: "活动ID不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "小程序用户ID不能为空", trigger: "blur" }
        ],
        documentType: [
          { required: true, message: "证件类型，默认为居民身份证不能为空", trigger: "change" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" }
        ],
        registrationFee: [
          { required: true, message: "报名费用,单位分不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态: 1(待审核)/2(已通过)/3(已付款)/4(已抽选)/5(已通知)/6(排队中)/7(已取消)/8(审核不通过)不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参赛者报名列表 */
    getList() {
      this.loading = true;
      listParticipants(this.queryParams).then(response => {
        this.participantsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ssParticipantsId: null,
        activityId: null,
        userId: null,
        documentType: null,
        documentNumber: null,
        country: null,
        name: null,
        gender: null,
        birthDate: null,
        phone: null,
        emergencyContact: null,
        emergencyPhone: null,
        contactRelation: null,
        bloodType: null,
        clothingSize: null,
        address: null,
        signature: null,
        registrationFee: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ssParticipantsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参赛者报名";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ssParticipantsId = row.ssParticipantsId || this.ids
      getParticipants(ssParticipantsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参赛者报名";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ssParticipantsId != null) {
            updateParticipants(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParticipants(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ssParticipantsIds = row.ssParticipantsId || this.ids;
      this.$modal.confirm('是否确认删除参赛者报名编号为"' + ssParticipantsIds + '"的数据项？').then(function() {
        return delParticipants(ssParticipantsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ss/participants/export', {
        ...this.queryParams
      }, `participants_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
